home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS 1.31 / testcpp / 9 / test.g < prev   
Encoding:
Text File  |  1995-03-10  |  845 b   |  55 lines  |  [TEXT/MPS ]

  1. /* This is test.g which tests simple AST refs and construction */
  2.  
  3. <<
  4. typedef ANTLRCommonToken ANTLRToken;
  5. #include "DLGLexer.h"
  6.  
  7. class AST : public ASTBase {
  8. public:
  9.     ANTLRToken token;
  10.     AST(ANTLRToken *t) { token = *t; }
  11.     void preorder_action() {
  12.         char *s = token.getText();
  13.         printf(" %s", s);
  14.     }
  15. };
  16.  
  17. int main()
  18. {
  19.     ANTLRToken aToken;
  20.     DLGFileInput in(stdin);
  21.     DLGLexer scan(&in,2000);
  22.     ANTLRTokenBuffer pipe(&scan);
  23.     scan.setToken(&aToken);
  24.     Expr parser(&pipe);
  25.     parser.init();
  26.  
  27.     ASTBase *root = NULL;
  28.     parser.e(&root);
  29.     root->preorder();
  30.     printf("\n");
  31.     return 0;
  32. }
  33. >>
  34.  
  35. #token "[\ \t\n]+"    <<skip();>>
  36. #token Eof "@"
  37.  
  38. class Expr {                /* Define a grammar class */
  39.  
  40. e    :    mult_expr ( ("\+"^|"\-"^) mult_expr )*
  41.     ;
  42.  
  43. mult_expr
  44.     :    atom ( ("\*"^|"\/"^) atom )*
  45.     ;
  46.  
  47. atom:    IDENTIFIER
  48.     |    NUMBER
  49.     ;
  50.  
  51. }
  52.  
  53. #token IDENTIFIER    "[a-z]+"
  54. #token NUMBER        "[0-9]+"
  55.